package com.oldwine.order.controller;

import com.oldwine.feign.clients.UserClient;
import com.oldwine.feign.pojo.Order;
import com.oldwine.feign.pojo.User;
import com.oldwine.order.config.PatternProperties;
import com.oldwine.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/order")
public class OrderCOntroller {
    @Autowired
    private OrderService orderService;

    @Autowired
    private PatternProperties properties;

    @Autowired
    private UserClient userClient;

    /**
     * 根据订单id查询订单信息
     * get请求
     * @param orderId 订单id
     * @return 返回订单对象
     */
    @GetMapping("/{orderId}")
    public Order findOrderById(@PathVariable("orderId") Long orderId){
        Order order = orderService.findOrderById(orderId);
        User user = userClient.findUserById(order.getUserId());
        order.setUser(user);
        return order;
    }

    /**
     * 测试订单微服务的热更新
     * @return
     */
    @GetMapping("/test")
    public String test(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));
    }
}
